/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.net.dreamlu.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package netty.mqtt.publish;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.codec.ByteBufferUtil;
import net.dreamlu.iot.mqtt.core.client.IMqttClientMessageListener;
import net.dreamlu.iot.mqtt.core.client.MqttClient;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class MqttPublisher {
    public static void main(String[] args) {
        // 初始化 mqtt 客户端
        MqttClient client = MqttClient.create()
                .ip("127.0.0.1")
                .port(1883)
                .connect();

        // 初始化 mqtt 客户端
        MqttClient emqClient = MqttClient.create()
                .ip("10.218.223.247")
                .port(1883)
                .connect();

        ScheduledExecutorService schedule = Executors.newScheduledThreadPool(4);
        schedule.scheduleAtFixedRate(
                new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String socket = FileUtils.readFileToString(new File("D:\\spring-demo\\tool-example\\src\\main\\java\\netty\\mqtt\\publish\\socket.json"),
                                    Charset.defaultCharset());
                            String devices = FileUtils.readFileToString(new File("D:\\spring-demo\\tool-example\\src\\main\\java\\netty\\mqtt\\publish\\devices.json"),
                                    Charset.defaultCharset());
//                            client.publish("/api/device/notify", ByteBuffer.wrap(devices.getBytes()));
//                            client.publish("/api/plc/data", ByteBuffer.wrap(json.getBytes()));
//                            client.publish("/api/device/oil", ByteBuffer.wrap(json.getBytes()));
//                            client.publish("device/drinkblender/pub", ByteBuffer.wrap(json.getBytes()));
//                            client.publish("/api/device/cmd", ByteBuffer.wrap(json.getBytes()));
//                            client.publish("/api/socket/data", ByteBuffer.wrap(socket.getBytes()));
//                            log.info("发送数据:{}", json);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }, 0, 60, TimeUnit.SECONDS);

        client.subQos1("device/smartsocket/data/Mc-943cc6a006a8", new IMqttClientMessageListener() {
            @Override
            public void onMessage(String topic, ByteBuffer payload) {
                emqClient.publish(topic, payload);
            }
        });

        emqClient.subQos1("device/smartsocket/data", new IMqttClientMessageListener() {
            @Override
            public void onMessage(String topic, ByteBuffer payload) {
                String data = ByteBufferUtil.toString(payload);
                log.info("topic {} data:{}", topic, data);
                client.publish(topic, payload);
            }
        });
        emqClient.subQos1("device/smartsocket/ack", new IMqttClientMessageListener() {
            @Override
            public void onMessage(String topic, ByteBuffer payload) {
                String data = ByteBufferUtil.toString(payload);
                log.info("topic {} data:{}", topic, data);
                client.publish(topic, payload);
            }
        });

    }


}
